package com.itheima.reggie.common;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
@Component
public class OssClinet {
    @Value("${aliyun.endpoint}")
    private String endpoint;
    @Value("${aliyun.accessKeyId}")
    private String accessKeyId;
    @Value("${aliyun.accessKeySecret}")
    private String accessKeySecret;
    @Value("${aliyun.bucketName}")
    private String bucketName;
    public String onload(String fileName, FileInputStream fis){
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            ossClient.putObject(bucketName, fileName, fis);
        } catch (OSSException oe) {
            oe.printStackTrace();
        }catch (ClientException e){
            e.printStackTrace();
        }finally {
            if (ossClient!=null){
                ossClient.shutdown();
            }
        }return "https://"+bucketName+"."+endpoint+"/"+fileName;
    }

}
